package io.renren.modules.avanotes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.BizCodeEnum;
import io.renren.common.exception.RRException;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.ShiroUtils;
import io.renren.common.vo.output.EchartsGraphXYVo;
import io.renren.modules.avanotes.dao.ScheduleSummaryDao;
import io.renren.modules.avanotes.entity.ScheduleSummaryEntity;
import io.renren.modules.avanotes.entity.StudyScheduleEntity;
import io.renren.modules.avanotes.service.ScheduleSummaryService;
import io.renren.modules.avanotes.service.StudyScheduleService;
import io.renren.modules.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


//@Service("scheduleSummaryService")//默认只有一个实现，那就不写名称了直接使用默认的名称！
@Service
public class ScheduleSummaryServiceImpl extends ServiceImpl<ScheduleSummaryDao, ScheduleSummaryEntity> implements ScheduleSummaryService {

    @Autowired
    StudyScheduleService studyScheduleService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ScheduleSummaryEntity> page = this.page(
                new Query<ScheduleSummaryEntity>().getPage(params),
                new QueryWrapper<ScheduleSummaryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Integer insert(ScheduleSummaryEntity param) {
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        //获取计划
        StudyScheduleEntity scheduleEntity = studyScheduleService.getById(param.getScheduleId());
        if (scheduleEntity == null) {
            throw new RRException(BizCodeEnum.SCHEDULE_NOT_EXIST.getMsg());
        }
        param.setCreateUserId(userEntity.getUserId());
        //生成日期 -- 改为计划的ID，因为总结的是针对当天的计划！而非创建日期
        //改为当天提交的日期！2022年7月24日
        param.setDay(Integer.parseInt(DateUtils.format(new Date(), DateUtils.DATE_PATTERN_INT)));
        //param.setDay(scheduleEntity.getDay());
        this.save(param);
        return param.getId();
    }

    @Override
    public ScheduleSummaryEntity infoByScheduleId(Integer scheduleId) {
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        ScheduleSummaryEntity entity = this.getOne(new QueryWrapper<ScheduleSummaryEntity>().eq(ScheduleSummaryEntity.CN_SCHEDULE_ID, scheduleId)
                .eq(ScheduleSummaryEntity.CN_CREATE_USER_ID, userEntity.getUserId()));
        return entity;
    }

    @Override
    public EchartsGraphXYVo aggPersonal() {
        EchartsGraphXYVo res = new EchartsGraphXYVo();
        //1.获取用户
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        //2.获取用户所有的总结信息
        List<ScheduleSummaryEntity> list = this.list(new QueryWrapper<ScheduleSummaryEntity>().eq(ScheduleSummaryEntity.CN_CREATE_USER_ID, userEntity.getUserId()).orderBy(true, true, ScheduleSummaryEntity.CN_DAY));
        List<String> xArias = new ArrayList<>();
        List<List<Object>> yAriasData = new ArrayList<>();
        for (ScheduleSummaryEntity entity : list) {
            String col = "" + entity.getDay();
            xArias.add(col);
            List<Object> obs = new ArrayList<>(3);
            obs.add(col);
            obs.add(entity.getGrade());
            obs.add(entity.getAverageScore());

            yAriasData.add(obs);
        }
        //
        res.setXAxis(xArias);
        res.setYAxisList(yAriasData);
        return res;
    }

}